#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f;

void solve()
{
    int n;
    string s;
    cin >> n >> s;
    int sum = 0;
    s = '0' + s;
    for (int i = 1; i <= n; i++) sum += s[i] != s[i - 1];
    s.resize(distance(s.begin(), unique(s.begin(), s.end())));
    int op = s.size();
    if (op > 3) cout << n + sum - 2 << endl;
    else if (op == 3 || op == 2) cout << n + 1 << endl;
    else cout << n + (s[0] == '1') << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}